package com.myacg.editor.common.unit.html;


import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

/**
 * 对html进行简单的封装 尽量还原HTML/css的操作方式 -->js的操作将被java的方式替代
 * @author Administrator
 *
 */
public class HtmlPage {
	private  HTMLDocument document;
	private  Element  body;
    public HtmlPage(){
    	//构造一个初始化的document
    	this.document  =(HTMLDocument)(new HTMLEditorKit().createDefaultDocument());
    	this.body = document.getRootElements()[0].getElement(0);
    }
    
    
    public Element getBody(){
    	return body;
    }
    public HTMLDocument getDocument() {
		return document;
	}
    /**
     * 在body最后插入一个节点
     * @param htmlText
     */
    public void appentElementToBody(String htmlText){
    	this.appentElement(body, htmlText);
    }
    /**
     * 
     * @param elem
     * @param htmlText
     */
    public void appentElement(Element elem,String htmlText){
    	try {
			this.document.insertBeforeEnd(elem, htmlText);
		} catch (Exception e) {
			e.printStackTrace();
		} 
    }
    public void setElementContent(Element elem,String htmlText){
    	try {
			this.document.setInnerHTML(elem, htmlText);
		} catch (Exception e) {
			e.printStackTrace();
		} 
    }
    
    public   String getText(Element element){
    	try {
			return this.document.getText(element.getStartOffset(), element.getEndOffset()-element.getStartOffset());
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    }
    /**
     * 输出文档当前的结构
     * @param elements
     * @param prefix
     * @throws BadLocationException 
     */
    public void printElements(Element element,String prefix) throws BadLocationException{
	    	//过滤掉空的content节点
    	    if(element.getEndOffset() - element.getStartOffset() >1){
				System.out.print(prefix+element.getName()+" ");
				if(element.getElementCount() ==  0 && element.getEndOffset() - element.getStartOffset() >1){
					int maxshow = 10;
					String text = getText(element);
					       text = text.length()>maxshow ? text.substring(0,maxshow)+"..." : text;
					System.out.println("---->"+ text  );
				}else{
					System.out.println();
				}
	    	}
			 for(int i =0 ;i<element.getElementCount();i++){
				printElements(element.getElement(i) , prefix+"  ");
			 }
			 
    }
    
	public static void main(String[] args) throws BadLocationException {
		HtmlPage htmlPage = new HtmlPage();
		 ElementBlock h1 = new ElementBlock("div");
		 h1.addElementChild("form", "p标签中的内容")
		   .addElementChild("span", "span标签中的内容");
//		   .addInputElementChild("text", "测试textinput框")
//		   .addInputElementChild("button", "测试button框");
		   
		 System.out.println(h1.getInnerHtml());
		 htmlPage.appentElementToBody(h1.getInnerHtml());
		htmlPage.appentElementToBody(new InputElementBlock("text","123").getInnerHtml());
		//输出文档当前的结构
		htmlPage.printElements(htmlPage.document.getRootElements()[0], "");
	} 
}
